﻿ //Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Data;
using System.Reflection;
using System.Text.RegularExpressions;
using FinPlusWCFConnData;
using FinPlusCommon;
using log = Logger.Logger;
using u = FinPlusUtility.Utility;

namespace FinPlusWPFControls
{
    public sealed class  FinPlusParentService:FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { }; } }
        public override string[] TestStates { get { return new string[] { "ModelTest" }; } }
        public override IFunctions Model { get { return _conn.GetFunctions(_serviceName); } }
        public override bool IsValid { get; set; }

        public override string ModelTest { get { return Model.ToString(); } }
        public bool CanWrite { get { return _canWrite; } }

        private enum Params { Group, Query };
        private FinPlusServiceConn _conn = new FinPlusServiceConn();
        private readonly string _serviceName, _serviceKey, _keyFormat, _parms;
        private bool _canWrite = false;

        //construct
        public FinPlusParentService(string serviceName, string serviceType, string keyFormat, string xmlPath, string node, bool isTransposed, bool isNew)
        {
            _serviceKey = FinPlusServiceConn.GetServiceKey(_serviceName = serviceName, _parms = string.Empty, _keyFormat = keyFormat);

            if (isNew) _conn.Dispose(serviceName);
            
            if (xmlPath == "root") xmlPath = Assembly.GetEntryAssembly().Location.ToLower().Replace(".exe", ".client");

            _conn.Connect(Env, Config, User, serviceType, _serviceKey, xmlPath, node, isTransposed);
            
            IsValid = true;
        }

        public FinPlusParentService(string serviceName, string serviceType, string parms, string keyFormat ="[group],[type],[where]", bool isTransposed = true)
        {
            _serviceKey = FinPlusServiceConn.GetServiceKey(_serviceName = serviceName, _parms = parms, _keyFormat = keyFormat);

            var regx = Regex.Match(parms, @"\[group\],(?<Group>.*?)\;");
            if (!regx.Groups[Params.Group.ToString()].Success)
                throw new Exception(string.Format("Service type {0} name {1} has params format problem", serviceType, _serviceName));

            var group = regx.Groups[Params.Group.ToString()].Value.ToString();

            var groupPerm = GetPermission(Params.Group.ToString(), group);
            if (!groupPerm.CanRead)
                throw new Exception(string.Format("User {0} permission problem, 'Group' ({1} {2})", User, regx.Groups["Group"].Value.ToString(), groupPerm.ToString()));
  
            GetPermissions(group);

            _conn.Connect(Env, Config, User, _serviceKey, serviceType, _parms, isTransposed);
            
            IsValid = true;
        }

        internal FinPlusParentService(string serviceName, string parms, string keyFormat, bool isTransposed = true, bool isAsync = false)
        {
            _serviceKey = FinPlusServiceConn.GetServiceKey(_serviceName = serviceName, _parms = parms, _keyFormat = keyFormat);

            _conn.Connect(Env, Config, User, _serviceKey, Params.Query.ToString(), _parms.TrimEnd(';'), isTransposed, isAsync);
        }

        internal void Save(IFunction func)
        {
            _conn.Save(_serviceKey, func);
        }

        //common control interface 
        public override void CommandRouter(IFunction func)
        {
            try
            {
                throw new NotImplementedException();
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose()
        {
            _conn.Dispose(_serviceKey);
        }

        //public
        public FuncDynamics GetFuncsTransposed()
        {
            return _conn.GetFuncDynamics(_serviceKey);
        }

        public IFunctions GetFunctions()
        {
            return _conn.GetFunctions(_serviceKey);
        }

        //private 
        private void GetPermissions(string group)
        {
            var perm = base.GetPermission(Params.Group.ToString(), group);
            if (!perm.CanRead)
                throw new Exception(string.Format("User {0} permission problem, 'Group {1}", User, group));
            _canWrite =  perm.CanWrite;
        }
    }
}


























































































//Copyright2013<jnrton>AllRightsReserved 
//Contact j.nrton@finpus